local hx__guanyi = fk.CreateSkill {
  name = "hx__guanyi",
  dynamic_desc = function(self, player)
    if player:getMark("hx__guanyi-change1") > 0 and player:getMark("hx__guanyi-change2") == 0 then
      return "hx__guanyi_change1"
    elseif player:getMark("hx__guanyi-change2") > 0 and player:getMark("hx__guanyi-change1") == 0 then
      return "hx__guanyi_change2"
    elseif player:getMark("hx__guanyi-change1") > 0 and player:getMark("hx__guanyi-change2") > 0 then
      return "hx__guanyi_change12"
    else
      return "hx__guanyi"
    end
  end,
}


Fk:loadTranslationTable{
  [":hx__guanyi_change1"] = "你失去牌后，你可以执行一项："..
  "<br/>1.失去一点体力；"..
  "<br/>2.获得一个“贤”；"..
  "<br/>3.回复一点体力。"..
  "<br/>然后若此牌是你的“贤”因此技能而变动后你使用或打出的第X张牌（包括第一张），你从牌堆底摸Y张牌或将牌堆顶的Y张牌交给一名其他角色（X为你的“贤”的个数，Y为你的体力值且至少为1）。",
  [":hx__guanyi_change2"] = "你使用或打出牌后，你可以执行一项："..
  "<br/>1.失去一点体力；"..
  "<br/>2.获得一个“贤”；"..
  "<br/>3.回复一点体力。"..
  "<br/>然后若此牌是你的“贤”因此技能而变动后你失去的第X张牌（包括第一张），你从牌堆底摸Y张牌或将牌堆顶的Y张牌交给一名其他角色（X为你的“贤”的个数，Y为你的体力值且至少为1）。",
  [":hx__guanyi_change12"] = "你失去牌后，你可以执行一项："..
  "<br/>1.失去一点体力；"..
  "<br/>2.获得一个“贤”；"..
  "<br/>3.回复一点体力。"..
  "<br/>然后若此牌是你的“贤”因此技能而变动后你失去的第X张牌（包括第一张），你从牌堆底摸Y张牌或将牌堆顶的Y张牌交给一名其他角色（X为你的“贤”的个数，Y为你的体力值且至少为1）。",
}


hx__guanyi:addEffect(fk.AfterCardsMove,{
  anim_type = "control",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__guanyi.name) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if player:getMark("hx__guanyi-change1") == 0 then
                return move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResponse
              elseif player:getMark("hx__guanyi-change1") > 0 then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, hx__guanyi.name)
    player:broadcastSkillInvoke(hx__guanyi.name, math.random(5,6))
    local choices = {"hx__guanyi_lost", "hx__guanyi_get" }
    if player:isWounded() then
      table.insert(choices, "hx__guanyi_recover")
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = hx__guanyi.name,
      cancelable = false,
    })
    if choice == "hx__guanyi_recover" then
      room:recover({ num = 1, skillName = hx__guanyi.name, who = player, recoverBy = player })
    elseif choice == "hx__guanyi_lost" then
      room:loseHp(player, 1, hx__guanyi.name)
    elseif choice == "hx__guanyi_get" then
      room:setPlayerMark(player, "hx__guanyi-compare", player:getMark("@@hx__guanyi-can"))
      room:addPlayerMark(player,"@yanjun__xian", 1)
      room:addPlayerMark(player, "@@hx__guanyi-can", 1)
      room:setPlayerMark(player, "@hx__guanyi-count", 0)
    end
    local x = player:getMark("@yanjun__xian")
    local y = player:getMark("@hx__guanyi-count")
    if x == y and not player.dead and player:getMark("@@hx__guanyi-can") ~= player:getMark("hx__guanyi-compare") then
      local chosen = room:askToChoice(player, {
        choices = {"#hx__guanyi-1","#hx__guanyi-2"},
        skill_name = hx__guanyi.name,
        cancelable = false,
      })
      if chosen == "#hx__guanyi-1" then
        room:notifySkillInvoked(player, hx__guanyi.name)
        player:broadcastSkillInvoke(hx__guanyi.name, math.random(1,2))
        player:drawCards(math.max(1, player.hp),hx__guanyi.name,"bottom")
      elseif chosen == "#hx__guanyi-2" then
        room:notifySkillInvoked(player, hx__guanyi.name)
        player:broadcastSkillInvoke(hx__guanyi.name, math.random(3,4))
        local target = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player),
          min_num = 1,
          max_num = 1,
          skill_name = hx__guanyi.name,
          cancelable = false,
          prompt = "将牌堆顶的Y张牌交给选择的角色（Y为你的体力值且至少为1）",
        })
        if #target > 0 then
          room:moveCards({
            ids = room:getNCards(math.max(1, player.hp)),
            from = player,
            to = target[1],
            toArea = Card.PlayerHand,
            moveReason = fk.ReasonGive,
            proposer = player,
            skillName = hx__guanyi.name,
          })
        end
      end
    end
  end,
})

hx__guanyi:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  priority = 0.1,
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__guanyi.name) and player:getMark("@@hx__guanyi-can") ~= player:getMark("hx__guanyi-compare") then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if player:getMark("hx__guanyi-change2") == 0 then
                return move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResonpse
              elseif player:getMark("hx__guanyi-change2") > 0 then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@hx__guanyi-count", 1)
  end,
})



return hx__guanyi